/**
 * This file is part of the dspace-poltman project http://www.poltman.com.
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of DuraSpace DSpace license detailed in the LICENSE and NOTICE 
 * files at the root of the source tree and available online at
 * http://www.dspace.org/license/
 * 
 */

package com.poltman.dscentral.communities;

import java.util.Set;

import org.apache.log4j.Logger;
import org.zkoss.zkplus.spring.SpringUtil;
import org.zkoss.zul.AbstractTreeModel;

import com.poltman.dscentral.core.CoreSessionData;
import com.poltman.dscentral.core.dspace.Communities;

/**
 * 
 * @author z.ciok@poltman.com
 *
 */
public class CommunityUtils {

	private static Logger log = Logger.getLogger(CommunityUtils.class);
	
	/**
	 * 
	 * @param communities
	 * @throws  
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void refreshCommunityTree(Communities communities) throws Exception {
		try {
			communities.getTree().setModel(new CommunityTreeModel());
			communities.getTree().setItemRenderer(new CommunityTreeRenderer(communities)); 
			
        	AbstractTreeModel ctm = (AbstractTreeModel)(communities.getTree().getModel());
        	CoreSessionData coreSessionData = (CoreSessionData) SpringUtil.getBean("coreSessionData");
        	Set<Integer> open = (Set)coreSessionData.getAttribute("CommunityOpenedTreeObjects");        	
			ctm.setOpenObjects(open);
        				
		} catch (Exception e) {
			log.error("refreshCommunityTree ", e);
			throw e;
		}
	}
	
	/**
	 * 
	 * @param communities
	 * @throws  
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void refreshNewTopLevelCommunityTree(Communities communities) throws Exception {
		try {
			communities.getTree().setModel(new CommunityTreeModel());
			communities.getTree().setItemRenderer(new NewTopLevelCommunityTreeRenderer(communities)); 
			
        	AbstractTreeModel ctm = (AbstractTreeModel)(communities.getTree().getModel());
        	CoreSessionData coreSessionData = (CoreSessionData) SpringUtil.getBean("coreSessionData");
        	Set<Integer> open = (Set) coreSessionData.getAttribute("NewTopLevelCommunityOpenedTreeObjects");
			ctm.setOpenObjects(open);
        				
		} catch (Exception e) {
			log.error("refreshCommunityTree ", e);
			throw e;
		}
	}
	
}
